#include "RunBusinessPart.h"
#include "Player.h"
#include "PlayerMgr.h"
#include "PackagePart.h"
#include "Common/TableData/ItemItemCfg.h"
#include "Common/PackageDefine.h"
#include "Common/TableDataEx/ItemCfg.h"
#include "Common/TableData/GuildRunbusinessCfg.h"
#include "Common/TableData/ConstantConstantCfg.h"
#include "Common/TableData/FunctionunlockFunctionUnlockCfg.h"
#include "HuoYueDuPart.h"

using namespace ProtoBuf;

RunBusinessPart::RunBusinessPart()
{
}

RunBusinessPart::~RunBusinessPart()
{
}

bool RunBusinessPart::SaveDB(CharacterDBInfo& characterDB)
{

	return true;
}

bool RunBusinessPart::Init(Creature *pMaster, uint32_t partType, CharacterDBInfo* pCharacterDB /* = nullptr */)
{
	Part::Init(pMaster, partType);

	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_GUILD_CHANGE, m_pMaster->GetCid(), CREATURE_PLAYER, NULL);

	return true;
}

bool RunBusinessPart::UnInit()
{
	g_GetEvent()->UnSubscribeAll(this);

	return true;
}

void RunBusinessPart::OnExecute(uint16_t nEventID, uint64_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen)
{
	switch (nEventID)
	{
		case EVENT_GUILD_CHANGE:
		{
			InitRunBusiness();
			break;
		}
		default:
			break;
	}
}

bool RunBusinessPart::OnMsgHandle(uint32_t cmd, const char* data, uint32_t len)
{
	switch (cmd)
	{
	case CLIENT_TO_LOGIC_OPENRUNBUSINESS:
	{
		HandleOpenRunBusinessReq(data, len);
		break;
	}
	case CLIENT_TO_LOGIC_RECEIVERUNBUSINESS:
	{
		HandleReceiveRunBusinessReq(data, len);
		break;
	}
	case CLIENT_TO_LOGIC_SUBMITRUNBUSINESS:
	{
		HandleSubmitRunBusinessReq(data, len);
		break;
	}
	case CLIENT_TO_LOGIC_EXCHANGERUNBUSINESS:
	{
		HandleExchangeRunBusinessReq(data, len);
		break;
	}
	default:
		break;
	}
	return true;
}

bool RunBusinessPart::SynOnLogin()
{
	InitRunBusiness();
	return true;
}

void RunBusinessPart::OnChangeLogic()
{
	InitRunBusiness();
}

bool RunBusinessPart::HandleOpenRunBusinessReq(const char* data, uint32_t len)
{
	ProtoBuf::CGOpenRunBusinessReq openRunBusinessReq;
	ProtoBuf::GCOpenRunBusinessRsp openRunBusinessRsp;

	if (!openRunBusinessReq.ParseFromArray(data, len))
	{
		MMOLOG_FMT_ERROR("[logic] CGOpenRunBusinessReq parse failed...");
		return false;
	}

	Player* pPlayer = dynamic_cast<Player*>(m_pMaster);
	if (nullptr == pPlayer)
	{
		MMOLOG_FMT_ERROR("[logic] RunBusinessPart::HandleOpenRunBusinessReq but Player is nullptr");
		return false;
	}

	//判断等级是否满足
	const FunctionunlockFunctionUnlockCfgInfo * pRunbusiOpenLevel = g_GetFunctionunlockFunctionUnlockCfgTable()->GetFunctionunlockFunctionUnlockCfgInfo(FunctionUnlock_ID_TYPE_RUNBUSI);
	if (nullptr == pRunbusiOpenLevel)
	{
		openRunBusinessRsp.set_ret(RET_CONFIG_ERROR);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_OPENRUNBUSINESS, &openRunBusinessRsp);
		return false;
	}

	uint32_t level = pPlayer->GetAttr(C_LEVEL);
	if (level < (uint32_t)pRunbusiOpenLevel->unlockLevel)
	{
		openRunBusinessRsp.set_ret(RET_LEVEL_LACK);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_OPENRUNBUSINESS, &openRunBusinessRsp);
		return false;
	}

	//判断角色是否有加入了工会
	if (pPlayer->GetAttr(C_UNION_ID) <= 0)
	{
		openRunBusinessRsp.set_ret(RET_CAN_NOT_ACCEPT_NOT_HAVE_UNION);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_OPENRUNBUSINESS, &openRunBusinessRsp);
		return false;
	}

	//获取工会等级对应的商券列表
	VEC_UINT32 businessIds;
	businessIds.clear();
	GetBusinessIds(businessIds);

	openRunBusinessRsp.set_ret(RET_SUCCESS);
	//将商券列表下发给客户端
	for (uint32_t i = 0; i < businessIds.size(); i++)
	{
		openRunBusinessRsp.add_businessid(businessIds[i]);
	}

	pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_OPENRUNBUSINESS, &openRunBusinessRsp);
	return true;
}

bool RunBusinessPart::HandleReceiveRunBusinessReq(const char* data, uint32_t len)
{
	ProtoBuf::CGReceiveRunBusinessReq receiveRunBusinessReq;
	ProtoBuf::GCReceiveRunBusinessRsp receiveRunBusinessRsp;

	if (!receiveRunBusinessReq.ParseFromArray(data, len))
	{
		MMOLOG_FMT_ERROR("[logic] CGReceiveRunBusinessReq parse failed...");
		return false;
	}

	Player* pPlayer = dynamic_cast<Player*>(m_pMaster);
	if (!pPlayer)
	{
		MMOLOG_FMT_ERROR("[logic] RunBusinessPart::HandleReceiveRunBusinessReq but Player is nullptr");
		return false;
	}

	//判断等级是否满足
	const FunctionunlockFunctionUnlockCfgInfo * pRunbusiOpenLevel = g_GetFunctionunlockFunctionUnlockCfgTable()->GetFunctionunlockFunctionUnlockCfgInfo(FunctionUnlock_ID_TYPE_RUNBUSI);
	if (nullptr == pRunbusiOpenLevel)
	{
		receiveRunBusinessRsp.set_ret(RET_CONFIG_ERROR);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_RECEIVERUNBUSINESS, &receiveRunBusinessRsp);
		return false;
	}

	uint32_t level = pPlayer->GetAttr(C_LEVEL);
	if (level < (uint32_t)pRunbusiOpenLevel->unlockLevel)
	{
		receiveRunBusinessRsp.set_ret(RET_LEVEL_LACK);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_RECEIVERUNBUSINESS, &receiveRunBusinessRsp);
		return false;
	}

	//判断角色是否有加入了工会
	if (pPlayer->GetAttr(C_UNION_ID) <= 0)
	{
		receiveRunBusinessRsp.set_ret(RET_CAN_NOT_ACCEPT_NOT_HAVE_UNION);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_RECEIVERUNBUSINESS, &receiveRunBusinessRsp);
		return false;
	}

	//判断是否接取对应的跑商任务
	uint32_t businessId = receiveRunBusinessReq.businessid();
	//TODO 防止客户端作弊，此处判断公会商会等级(中心服)
	/*bool isCanAccept = CanAcceptRunBusiness(businessId);
	if (!isCanAccept)
	{
		receiveRunBusinessRsp.set_ret(RET_CAN_NOT_ACCEPT_UNION_LV_LIMIT);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_RECEIVERUNBUSINESS, &receiveRunBusinessRsp);
		return false;
	}*/

	//可以接取的话往背包增加物品
	Part* pPart = pPlayer->GetPart(PART_PACKAGE);
	if (!pPart)
	{
		receiveRunBusinessRsp.set_ret(RET_DATA_ERROR);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_RECEIVERUNBUSINESS, &receiveRunBusinessRsp);
		return false;
	}

	//判断是否接取过了跑商物品了
	CItemBase* pItemBase = const_cast<CItemBase*>(pPart->GetItemByType((int32_t)EItemSubType::EItemSubType_run_business));
	if (nullptr != pItemBase)
	{
		receiveRunBusinessRsp.set_ret(RET_CAN_NOT_RECEIVE_HAVE_BUSINESS);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_RECEIVERUNBUSINESS, &receiveRunBusinessRsp);
		return false;
	}
	
	//增加商券物品
	LIST_ITEM lstItem;
	SItem item;
	item.nItemID = businessId;
	item.nNum = 1;
	lstItem.push_back(item);
	SItemSourceParam sourceParam; 
	sourceParam.source = S_RunBusiness;
	if (!pPart->AddItem(lstItem, sourceParam))
	{
		receiveRunBusinessRsp.set_ret(RET_BAG_FULL);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_RECEIVERUNBUSINESS, &receiveRunBusinessRsp);
		return false;
	}

	receiveRunBusinessRsp.set_ret(RET_SUCCESS);
	pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_RECEIVERUNBUSINESS, &receiveRunBusinessRsp);
	return true;
}

bool RunBusinessPart::HandleSubmitRunBusinessReq(const char* data, uint32_t len)
{
	ProtoBuf::CGSubmitRunBusinessReq submitRunBusinessReq;
	ProtoBuf::GCSubmitRunBusinessRsp submitRunBusinessRsp;

	if (!submitRunBusinessReq.ParseFromArray(data, len))
	{
		MMOLOG_FMT_ERROR("[logic] CGSubmitRunBusinessReq parse failed...");
		return false;
	}

	Player* pPlayer = dynamic_cast<Player*>(m_pMaster);
	if (!pPlayer)
	{
		MMOLOG_FMT_ERROR("[logic] RunBusinessPart::CGSubmitRunBusinessReq but Player is nullptr");
		return false;
	}

	//判断等级是否满足
	const FunctionunlockFunctionUnlockCfgInfo * pRunbusiOpenLevel = g_GetFunctionunlockFunctionUnlockCfgTable()->GetFunctionunlockFunctionUnlockCfgInfo(FunctionUnlock_ID_TYPE_RUNBUSI);
	if (nullptr == pRunbusiOpenLevel)
	{
		submitRunBusinessRsp.set_ret(RET_CONFIG_ERROR);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_SUBMITRUNBUSINESS, &submitRunBusinessRsp);
		return false;
	}

	uint32_t level = pPlayer->GetAttr(C_LEVEL);
	if (level < (uint32_t)pRunbusiOpenLevel->unlockLevel)
	{
		submitRunBusinessRsp.set_ret(RET_LEVEL_LACK);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_SUBMITRUNBUSINESS, &submitRunBusinessRsp);
		return false;
	}

	//判断角色是否有加入了工会
	if (pPlayer->GetAttr(C_UNION_ID) <= 0)
	{
		submitRunBusinessRsp.set_ret(RET_CAN_NOT_ACCEPT_NOT_HAVE_UNION);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_SUBMITRUNBUSINESS, &submitRunBusinessRsp);
		return false;
	}
	
	uint32_t businessId = submitRunBusinessReq.businessid();
	//判断背包是否有该物品
	Part* pPart = pPlayer->GetPart(PART_PACKAGE);
	if (!pPart)
	{
		submitRunBusinessRsp.set_ret(RET_DATA_ERROR);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_SUBMITRUNBUSINESS, &submitRunBusinessRsp);
		return false;
	}

	//获取物品配置表
	const ItemItemCfgInfo* itemCfgInfo = g_GetItemItemCfgTable()->GetItemItemCfgInfo(businessId);
	if (!itemCfgInfo)
	{
		submitRunBusinessRsp.set_ret(RET_DATA_ERROR);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_SUBMITRUNBUSINESS, &submitRunBusinessRsp);
		return false;
	}

	//检查背包是否有商券物品
	LIST_ITEM lstItem;
	SItem item;
	item.nItemID = businessId;
	item.nNum = 1;
	lstItem.push_back(item);
	if (!pPart->HasItem(lstItem))
	{
		submitRunBusinessRsp.set_ret(RET_CAN_NOT_SUMBIT_NOT_HAVE_BUSINESS);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_SUBMITRUNBUSINESS, &submitRunBusinessRsp);
		return false;
	}

	//检查背包是否还存在跑商交易物品
	CItemBase* pTradeItemBase = const_cast<CItemBase*>(pPart->GetItemByType((int32_t)EItemSubType::EItemSubType_run_business_trade));
	if (nullptr != pTradeItemBase)
	{
		SItemSourceParam sourceParam;
		sourceParam.source = S_RunBusiness;
		pPart->RemoveAllByType((int32_t)EItemSubType::EItemSubType_run_business_trade, sourceParam);
		//submitRunBusinessRsp.set_ret(RET_CAN_NOT_SUMBIT_HAVE_TRADE_ITEM);
		//pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_SUBMITRUNBUSINESS, &submitRunBusinessRsp);
		//return false;
	}

	//检查目标是否达成
	CItemBase* pItemBase = const_cast<CItemBase*>(pPart->GetItemByType((int32_t)EItemSubType::EItemSubType_run_business));
	if (nullptr != pItemBase)
	{
		int32_t fund = pItemBase->GetFund();
		const VEC_INT32 *pvecPara = g_GetItemCfgMgr()->GetItemFuncParamCfg(businessId);
		if (nullptr != pvecPara)
		{
			if (pvecPara->size() >= 1)
			{
				if (fund < pvecPara->at(0))
				{
					submitRunBusinessRsp.set_ret(RET_CAN_NOT_SUMBIT_FUND_LIMIT);
					pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_SUBMITRUNBUSINESS, &submitRunBusinessRsp);
					return false;
				}
			}
		}
	}

	//目标达成扣除跑商物品并且给予奖励
	SItemSourceParam sourceParam;
	sourceParam.source = S_RunBusiness;
	pPart->RemoveItem(lstItem, sourceParam);
	SumbitReward(businessId);
	submitRunBusinessRsp.set_ret(RET_SUCCESS);
	pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_SUBMITRUNBUSINESS, &submitRunBusinessRsp);

	g_GetEvent()->FireExecute(EVENT_UNION_RUN_BUSINESS, m_pMaster->GetCid(), m_pMaster->CreatureKind(), nullptr, 0);

	HuoYueDuPart* pHuoYueDuPart = dynamic_cast<HuoYueDuPart*>(pPlayer->GetPart(PART_HUOYUEDU));
	if (nullptr != pHuoYueDuPart)
	{
		//增加活跃度
		pHuoYueDuPart->AddHuoYueDuRecord(HYDT_RUN_BUSINESS);
	}
	return true;
}

bool RunBusinessPart::HandleExchangeRunBusinessReq(const char* data, uint32_t len)
{
	ProtoBuf::CGExchangeRunBusinessReq exchangeRunBusinessReq;
	ProtoBuf::GCExchangeRunBusinessRsp exchangeRunBusinessRsp;

	if (!exchangeRunBusinessReq.ParseFromArray(data, len))
	{
		MMOLOG_FMT_ERROR("[logic] CGExchangeRunBusinessReq parse failed...");
		return false;
	}

	Player* pPlayer = dynamic_cast<Player*>(m_pMaster);
	if (!pPlayer)
	{
		MMOLOG_FMT_ERROR("[logic] RunBusinessPart::HandleExchangeRunBusinessReq but Player is nullptr");
		return false;
	}

	Part* pPart = pPlayer->GetPart(PART_PACKAGE);
	if (!pPart)
	{
		exchangeRunBusinessRsp.set_ret(RET_DATA_ERROR);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
		return false;
	}

	//判断等级是否满足
	const FunctionunlockFunctionUnlockCfgInfo * pRunbusiOpenLevel = g_GetFunctionunlockFunctionUnlockCfgTable()->GetFunctionunlockFunctionUnlockCfgInfo(FunctionUnlock_ID_TYPE_RUNBUSI);
	if (nullptr == pRunbusiOpenLevel)
	{
		exchangeRunBusinessRsp.set_ret(RET_CONFIG_ERROR);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
		return false;
	}

	uint32_t level = pPlayer->GetAttr(C_LEVEL);
	if (level < (uint32_t)pRunbusiOpenLevel->unlockLevel)
	{
		exchangeRunBusinessRsp.set_ret(RET_LEVEL_LACK);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
		return false;
	}

	uint32_t type = exchangeRunBusinessReq.type();
	if (type < CGExchangeRunBusinessReq_EXCHANGETYPE_EXCHANGETYPE_COIN || type > CGExchangeRunBusinessReq_EXCHANGETYPE_EXCHANGETYPE_DIAMOND)
	{
		exchangeRunBusinessRsp.set_ret(RET_PARMAR_ERROR);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
		return false;
	}

	//判断角色是否有加入了工会
	if (pPlayer->GetAttr(C_UNION_ID) <= 0)
	{
		exchangeRunBusinessRsp.set_ret(RET_CAN_NOT_ACCEPT_NOT_HAVE_UNION);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
		return false;
	}

	//获取物品配置表
	uint32_t businessId = exchangeRunBusinessReq.businessid();
	const ItemItemCfgInfo* itemCfgInfo = g_GetItemItemCfgTable()->GetItemItemCfgInfo(businessId);
	if (!itemCfgInfo)
	{
		exchangeRunBusinessRsp.set_ret(RET_DATA_ERROR);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
		return false;
	}

	//检查背包是否有商券物品
	LIST_ITEM lstItem;
	SItem item;
	item.nItemID = businessId;
	item.nNum = 1;
	lstItem.push_back(item);
	if (!pPart->HasItem(lstItem))
	{
		exchangeRunBusinessRsp.set_ret(RET_CAN_NOT_EXCHANGE_HAVE_TRADE_ITEM);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
		return false;
	}

	//检查背包是否有商券物品
	CItemBase* pItemBase = const_cast<CItemBase*>(pPart->GetItemByType((int32_t)EItemSubType::EItemSubType_run_business));
	if (!pItemBase)
	{
		exchangeRunBusinessRsp.set_ret(RET_CAN_NOT_EXCHANGE_HAVE_TRADE_ITEM);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
		return false;
	}

	const VEC_INT32 *pvecPara = g_GetItemCfgMgr()->GetItemFuncParamCfg(businessId);
	if (!pvecPara || pvecPara->size() < 1)
	{
		exchangeRunBusinessRsp.set_ret(RET_CONFIG_ERROR);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
		return false;
	}

	int32_t fund = pItemBase->GetFund();
	int32_t totalFund = pvecPara->at(0);
	if (fund >= totalFund)
	{
		exchangeRunBusinessRsp.set_ret(RET_CAN_NOT_EXCHANGE_FUND_FULL);
		pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
		return false;
	}
	
	//未达成
	float needFund = totalFund - fund;
	if (type == CGExchangeRunBusinessReq_EXCHANGETYPE_EXCHANGETYPE_COIN)
	{
		const ConstantConstantCfgInfo *pGoldRatioCfg = g_GetConstantConstantCfgTable()->GetConstantConstantCfgInfo(EArg_RunBusiGoldRatio);
		if (nullptr == pGoldRatioCfg)
		{
			LogErrFmtPrint("[logic] HandleExchangeRunBusinessReq pGoldRatioCfg = nullptr.. failed....");
			exchangeRunBusinessRsp.set_ret(RET_CONFIG_ERROR);
			pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
			return false;
		}

		int64_t gold = pPlayer->GetAttr(C_GOLD);
		int64_t needGold = ceil(needFund / pGoldRatioCfg->constantdata);
		if (needGold > gold)
		{
			exchangeRunBusinessRsp.set_ret(RET_GOLD_LACK);
			pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
			return false;
		}
		else
		{
			m_pMaster->AddSourceAttr(C_GOLD, -1 * (int64_t)needGold, true, S_RunBusiness);
			pItemBase->SetFund(totalFund);
			pPart->SetRunBusiness(pItemBase->GetIndex(), pItemBase->GetFund());
		}
		
	}
	else
	{
		const ConstantConstantCfgInfo *pDiamondRatioCfg = g_GetConstantConstantCfgTable()->GetConstantConstantCfgInfo(EArg_RunBusiDiamondRatio);
		if (nullptr == pDiamondRatioCfg)
		{
			LogErrFmtPrint("[logic] HandleExchangeRunBusinessReq pDiamondRatioCfg = nullptr.. failed....");
			exchangeRunBusinessRsp.set_ret(RET_CONFIG_ERROR);
			pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
			return false;
		}

		int32_t diamond = pPlayer->GetAttr(C_DIAMOND);
		int32_t needDiamond = ceil(needFund / pDiamondRatioCfg->constantdata);
		if (needDiamond > diamond)
		{
			exchangeRunBusinessRsp.set_ret(RET_DIAMOND_LACK);
			pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
			return false;
		}
		else
		{
			pPlayer->AddSourceAttr(C_DIAMOND, -needDiamond, true, S_RunBusiness);
			pItemBase->SetFund(totalFund);
			pPart->SetRunBusiness(pItemBase->GetIndex(), pItemBase->GetFund());
		}
	}

	exchangeRunBusinessRsp.set_ret(RET_SUCCESS);
	pPlayer->SendDataToClient(EMODULE_ID_RUNBUSINESS, LOGIC_TO_CLIENT_EXCHANGERUNBUSINESS, &exchangeRunBusinessRsp);
	return true;
}

void RunBusinessPart::GetBusinessIds(VEC_UINT32& businessIds)
{
	uint32_t unionLv = m_pMaster->GetAttr(C_UNION_LEVEL);
	
	if (unionLv >= 1)
	{
		businessIds.push_back(RUN_BUSINESS_APPRENTICE);
	}

	if (unionLv >= 2)
	{
		businessIds.push_back(RUN_BUSINESS_SALESMAN);
	}

	if (unionLv >= 3)
	{
		businessIds.push_back(RUN_BUSINESS_MANAGER);
	}

	if (unionLv >= 4)
	{
		businessIds.push_back(RUN_BUSINESS_MASTER);
	}

	if (unionLv >= 5)
	{
		businessIds.push_back(RUN_BUSINESS_WARREN);
	}
}

bool RunBusinessPart::CanAcceptRunBusiness(uint32_t businessId)
{
	VEC_UINT32 businessIds;
	GetBusinessIds(businessIds);
	for (uint32_t i = 0; i < businessIds.size(); i++)
	{
		if (businessIds[i] == businessId)
		{
			return true;
		}
	}

	return false;
}

void RunBusinessPart::InitRunBusiness()
{
	uint32_t guildId = m_pMaster->GetAttr(C_UNION_ID);
	if (guildId <= 0)
	{
		Part* pPart = m_pMaster->GetPart(PART_PACKAGE);
		if (pPart)
		{
			//如果玩家退出了工会直接删除商券物品
			const CItemBase* pItemBase = pPart->GetItemByType((int32_t)EItemSubType::EItemSubType_run_business);
			if (pItemBase)
			{
				SItemSourceParam sourceParam;
				sourceParam.source = S_RunBusiness;
				pPart->RemoveItem(pItemBase->GetItemID(), 1, sourceParam);
			}
		}
	}
}

void RunBusinessPart::SumbitReward(uint32_t businessId)
{
	const GuildRunbusinessCfgInfo* pRunBusinessCfgInfo = g_GetGuildRunbusinessCfgTable()->GetGuildRunbusinessCfgInfo(businessId);
	if (nullptr != pRunBusinessCfgInfo)
	{
		m_pMaster->AddSourceAttr(C_GOLD, pRunBusinessCfgInfo->gold, true, S_RunBusiness);
		Part* pPart = m_pMaster->GetPart(PART_PACKAGE);
		if (pPart)
		{
			LIST_ITEM lstItem;
			SItem item;
			item.nItemID = pRunBusinessCfgInfo->box;
			item.nNum = 1;
			SItemSourceParam sourceParam;
			sourceParam.source = S_RunBusiness;
			pPart->AddItem(lstItem, sourceParam);
		}
		
		//通知中心服加工会资金以及贡献
		LC_UnionUpdateAttrReq req;
		req.set_charid(m_pMaster->GetCid());
		req.set_unionmoney(pRunBusinessCfgInfo->unionGold);
		req.set_unioncontri(pRunBusinessCfgInfo->unionOffer);
		req.set_source(S_RunBusiness);
		g_GetLogicService()->SendDataToCenter(EMODULE_ID_UNION, LOGIC_TO_CENTER_UNION_UPDATE_ATTR, &req);
	}
}

